package com.success.application.ground.activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseDetailActivity;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.myapplication.R;
import com.success.application.shop.ActivityListFragmentViewPager;
import com.success.application.shop.bean.HuoDong;
import com.success.application.user.AccountHelper;
import com.success.application.user.bean.User;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Type;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/22.
 */

public class FriendDetailActivity extends BaseDetailActivity<HuoDong> {
    private HuoDong mhuoDong;
    public static final String BUNDLE_KEY_HUODONG = "BUNDLE_KEY_HUODONG";
    //参加活动列表
    private ActivityListFragmentViewPager mPagerFrag;
    @Bind(R.id.iv_consult_face)
    ImageView logo;//活动图片
    @Bind(R.id.tv_activity_title)
    TextView name;//活动主题
    @Bind(R.id.tv_activity_time)
    TextView shijian;//时间
    @Bind(R.id.tv_address)
    TextView dizhi;//地址
    @Bind(R.id.tv_content)
    TextView beizhu;//备注
    @Bind(R.id.aixin_content)
    WebView content;//详情
    @Bind(R.id.bt_join)
    Button mjoin;//报名按钮
    @Override
    protected Type getType() {
        return new TypeToken<Result<HuoDong>>() {
        }.getType();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.shop_activity_detail;
    }
    @Override
    protected int getTitleRes() {
        return R.string.activity_details;
    }
    //从列表传参
    @Override
    protected boolean initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mhuoDong = (HuoDong) getIntent().getSerializableExtra(BUNDLE_KEY_HUODONG);
        if (mhuoDong == null) {
            Toast.makeText(this, "对象没找到", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    //
    @Override
    protected void initWidget() {
        super.initWidget();
        //实例化活动报名会员的列表
        mPagerFrag = new ActivityListFragmentViewPager();
        Bundle bundle = new Bundle();
        bundle.putSerializable("HUODONG",mhuoDong);
        mPagerFrag.setArguments(bundle);
        //把创建的fragment提交给Manager
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, mPagerFrag)
                .commit();
    }

    @Override
    protected void initData() {
        super.initData();
        Params p = new Params();
        p.put("id",mhuoDong.getId());
        p.put("type","hdzx");
        try {
            YuanbaoApi.getActivityDetails(p, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        Log.d("VACK", doc.toString());
        return doc.toString();
    }
    //点击活动报名
    @OnClick(R.id.bt_join)
    void onClickJoin() {
        Params p = new Params();
        User user = AccountHelper.getUser();
        p.put("model.userId","1");
        p.put("model.hdId",mhuoDong.getId());
        p.put("model.type","hdzx");
        try {
            YuanbaoApi.addBm(p, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void setData(Result<HuoDong> result) {
        //详情列表
        Map mMap = result.getData();
        //点击参加报名成功
        String mMap1 = result.getResult();

        if(mMap==null){
            if(mMap1.equals("success")){
                mPagerFrag.onTabReselect();
                mjoin.setText(getResources().getString(R.string.hdzx_isjoin));
                mhuoDong.setHdzx_bmrs(mhuoDong.getHdzx_bmrs()+1);
            }
        }
        if(mMap!=null){
            //是否报名
            if(mMap.get("hdzx_bmqk").equals("N")){
                mjoin.setText(getResources().getString(R.string.hdzx_nojoin));
            }else{
                mjoin.setText(getResources().getString(R.string.hdzx_isjoin));
            }
            if(mMap.get("hdzx_zt")!=null){
                name.setText(mMap.get("hdzx_zt").toString());
            }
            if(mMap.get("hdzx_hdtb")!=null){
                Glide.with(getApplication()).load(mMap.get("hdzx_hdtb").toString()).into(logo);
            }
            if(mMap.get("hdzx_hdsj")!=null){
                shijian.setText(mMap.get("hdzx_hdsj").toString());
            }
            if(mMap.get("hdzx_hddd")!=null){
                dizhi.setText(mMap.get("hdzx_hddd").toString());
            }
            if(mMap.get("hdzx_bz")!=null){
                beizhu.setText(mMap.get("hdzx_bz").toString());
            }
            if(mMap.get("hdzx_hdxq")!=null){
                //  加载、并显示HTML代码
                content.loadDataWithBaseURL("http://47.92.92.215:8087", getNewContent(mMap.get("hdzx_hdxq").toString()), "text/html" , "utf-8", null);
                content.getSettings().setJavaScriptEnabled(true);
                content.getSettings().setDomStorageEnabled(true);
                content.getSettings().setUseWideViewPort(true);
                content.getSettings().setBlockNetworkImage(false);
                content.getSettings().setLoadWithOverviewMode(true);
                if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
                    content.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }
                content.setWebChromeClient(new WebChromeClient());
            }
        }

    }
}
